package com.zs.leymovie.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnRadioGroupCheckedChange;
import com.zs.leymovie.MainActivity;
import com.zs.leymovie.R;
import com.zs.leymovie.adapter.MovieFgAdapter;
import com.zs.leymovie.bean.MovieComing;
import com.zs.leymovie.bean.MovieShowing;
import com.zs.leymovie.localcity.ActivitySelectCity;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * <p/>
 * 作者：舒纲聿
 * 时间：2015年8月17日20:53:28
 * 功能：影片界面-展示正在放映和即将放映
 */
public class MovieFragment extends Fragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener,View.OnClickListener {
    @ViewInject(R.id.movie_location_city)
    private TextView mLocationCity;
    @ViewInject(R.id.movie_rg_container)
    private RadioGroup mGroup;
    @ViewInject(R.id.movie_viewpager_container)
    private ViewPager mPager;
    private Fragment[] fragments;

    private String city_id;
    private String city_name;
    public static final int REQUEST_CODE = 99;   //请求码
    private double lng = 114.058000;   //默认深圳
    private double lat = 22.543800;

    private ArrayList<MovieShowing> movieShowings = new ArrayList<>();
    private ArrayList<MovieComing> movieComings = new ArrayList<>();

    public MovieFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_movie, container, false);
        ViewUtils.inject(this, view);
        initData();
        initView();
        mPager.setOnPageChangeListener(this);
        mGroup.setOnCheckedChangeListener(this);
        mLocationCity.setOnClickListener(this);
        return view;
    }

    //初始化数据
    private void initData() {
        if (fragments == null) {
            fragments = new Fragment[2];
        }
        city_id = ((MainActivity) getActivity()).getCity_id();
        city_name = ((MainActivity) getActivity()).getLngCityName();
        fragments[0] = new MovingFragment();
        fragments[1] = new ComingFragment();
    }

    //初始化视图
    private void initView() {
        mLocationCity.setText(city_name);
        MovieFgAdapter movieFgAdapter = new MovieFgAdapter(getChildFragmentManager(), fragments);
        mPager.setAdapter(movieFgAdapter);

    }

    @OnRadioGroupCheckedChange(R.id.movie_rg_container)
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < group.getChildCount(); i++) {    //遍历所有的单选按扭，一个按扭对应一个fragment
            Fragment fragment = fragments[i];            //拿到当前的Fragment对象
            if (group.getChildAt(i).getId() == checkedId) {
                //检查是否之前已经加载过，没有则加载
                mPager.setCurrentItem(i);

            }

        }
    }

    //viewPager 滑动监听
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton button = ((RadioButton) mGroup.getChildAt(position));
        if (!button.isChecked()) {
            ((RadioButton) mGroup.getChildAt(position)).setChecked(true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == REQUEST_CODE) {
            Bundle bundle = data.getExtras();
            mLocationCity.setText(bundle.getString("lngCityName"));  //得到城市名
            city_id = bundle.getString("cityid");                  //城市编号
            ((MainActivity) getActivity()).setCity_id(city_id);
            ((MainActivity) getActivity()).setLngCityName(bundle.getString("lngCityName"));

            lat = bundle.getDouble("lat", 22.543800);                  //得到伟度，默认深圳
            lng = bundle.getDouble("lng", 114.058000);                 //得到经度，默认深圳
            initData();   //重新定位
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), ActivitySelectCity.class);
        startActivityForResult(intent, 99);
    }

    @Override
    public void onResume() {
        super.onResume();
        mLocationCity.setText(((MainActivity) getActivity()).getLngCityName());
    }
}
